package com.hosa.Onlinemall.coupon.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hosa.Onlinemall.common.annotation.Log;
import com.hosa.Onlinemall.common.core.controller.BaseController;
import com.hosa.Onlinemall.common.core.domain.AjaxResult;
import com.hosa.Onlinemall.common.enums.BusinessType;
import com.hosa.Onlinemall.coupon.domain.Coupon;
import com.hosa.Onlinemall.coupon.service.ICouponService;
import com.hosa.Onlinemall.common.utils.poi.ExcelUtil;
import com.hosa.Onlinemall.common.core.page.TableDataInfo;

/**
nacos配置中心统一管理
 1）引入依赖，nacos-config
 2）创建bootstrap.yml配置文件，配置nacos配置中心地址
 2.4以上版本引入依赖
 3）添加dataid
 spring:
 application:
 name: Onlinemall-coupon
 4)添加配置
 5）动态配置
 @refreshScope
 @value"${}"

 2,细节
 1）命名空间
 用于区分开发，测试，生产等环境做隔离
 微服务间隔离，每个微服务一个命名空间
 2）配置集，所有配置的集合
 3）配置ID 类似文件名
 4）配置分组
 在每个微服务自己的命名空间下使用配置分组区分环境，dev，test,prod

 3,
 */

/**
 * 优惠券信息Controller
 * 
 * @author hosa
 * @date 2025-02-26
 */
@RefreshScope
@RestController
@RequestMapping("/coupon/coupon")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService couponService;

    @Value("${coupon.user.name}")
    private  String name;
    @Value("${user.age}")
    private  Integer age;

    @RequestMapping("/test")
    public AjaxResult test(){
        return AjaxResult.success().put("name", name).put("age", age);
    }

    @RequestMapping("/member/list")
    public AjaxResult membercoupons()
    {
        Coupon coupon = new Coupon();
        coupon.setCouponName("不要999只要9.9");
        return AjaxResult.success().put("coupons", Arrays.asList(coupon));
    }

    /**
     * 查询优惠券信息列表
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon)
    {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券信息列表
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:export')")
    @Log(title = "优惠券信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon)
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券信息数据");
    }

    /**
     * 获取优惠券信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(couponService.selectCouponById(id));
    }

    /**
     * 新增优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:add')")
    @Log(title = "优惠券信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:edit')")
    @Log(title = "优惠券信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon)
    {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券信息
     */
    @PreAuthorize("@ss.hasPermi('coupon:coupon:remove')")
    @Log(title = "优惠券信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponService.deleteCouponByIds(ids));
    }
}
